package com.diankangwo.controller;

import com.diankangwo.constant.resultMsg;
import com.diankangwo.dao.CollectionDAO;
import com.diankangwo.dao.CommodityDAO;
import com.diankangwo.dao.UserDAO;
import com.diankangwo.domain.Collection;
import com.diankangwo.domain.Commodity;
import com.diankangwo.domain.UserInfo;
import com.diankangwo.util.PageSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 用戶收藏控制器
 *
 * @author xiaoyu
 */
@Controller
@Api(description = "用戶收藏")
public class CollectionController {
    @Autowired
    private UserDAO userDao;

    @Autowired
    private CollectionDAO collectionDao;

    @Autowired
    private CommodityDAO commodityDAO;

    /**
     * 获取我的收藏列表
     *
     * @param accessToken token
     * @param type        分类
     * @return
     */
    @ApiOperation(value = "获取我的收藏列表", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accessToken", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(name = "type", value = "分类", required = true, dataType = "String")
    })
    @ResponseBody
    @RequestMapping(value = "/collection/list", method = RequestMethod.GET)
    public String list(String accessToken, Integer type, int pageNo, int pageSize) {
        UserInfo user = userDao.getUserByToken(accessToken);

        String code = "2";
        String msg = "处理异常！";
        PageSupport<Collection> connList = null;
        if (user != null) {
            connList = collectionDao.getCollectionByUid(user.getUid(), pageNo, pageSize, type);
            code = "1";
            if (connList != null) {
                msg = "查询成功！";
            } else {
                msg = "未查询到收藏信息！";
                return resultMsg.mapToJson(code, msg, null);
            }
        } else {
            msg = "未查询到用户信息！";
        }
        return resultMsg.mapToJson(code, msg, connList.getResultData() == null ? null : connList.getResultData());
    }

    /**
     * 删除指定收藏
     *
     * @param accessToken
     * @param commodityId 商品ID
     * @return
     */
    @ApiOperation(value = "删除指定收藏", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accessToken", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(name = "commodityID", value = "商品ID", required = true, dataType = "Integer")
    })
    @ResponseBody
    @RequestMapping(value = "/collection/del", method = RequestMethod.POST)
    public String del(String accessToken, Integer commodityId) {
        UserInfo user = userDao.getUserByToken(accessToken);
        String code = "2";
        String msg = "处理异常！";
        if (user != null) {
            if (collectionDao.deleteByCid(commodityId)) {
                code = "1";
                msg = "删除成功！";
            } else {
                msg = "操作异常，删除失败！";
            }
        } else {
            msg = "未查询到用户信息！";
        }
        return resultMsg.mapToJson(code, msg, null);
    }

    /**
     * 添加收藏
     *
     * @param accessToken
     * @param commodityId 商品ID
     * @return
     */
    @ApiOperation(value = "删除指定收藏", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accessToken", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(name = "commodityId", value = "商品ID", required = true, dataType = "Integer")
    })
    @ResponseBody
    @RequestMapping(value = "/collection/add", method = RequestMethod.POST)
    public String add(String accessToken, Integer commodityId) {
        UserInfo user = userDao.getUserByToken(accessToken);
        String code = "2";
        String msg = "处理异常！";
        if (user != null) {
            Commodity commodity = commodityDAO.find(commodityId);
            Collection collection = new Collection();
            collection.setAdd_date(new Date());
            collection.setClass_id(commodity.getTypeId());
            collection.setGoods_id(commodity.getId());
            collection.setName(commodity.getCommodityName());
            collection.setPic_url(commodity.getCommodityPicLarge());
            collection.setUid(user.getUid());
            collection.setCommodityPriceShop(commodity.getCommodityPriceShop());
            collection.setCommodityPriceUser(commodity.getCommodityPriceUser());
            collectionDao.saveOrUpdate(collection);
            code = "1";
            msg = "添加成功！";
        } else {
            msg = "未查询到用户信息！";
        }
        return resultMsg.mapToJson(code, msg, null);
    }

    /**
     * 查看是否收藏
     *
     * @param accessToken
     * @param commodityId 商品ID
     * @return
     */
    @ApiOperation(value = "查看是否收藏", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accessToken", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(name = "commodityId", value = "商品ID", required = true, dataType = "Integer")
    })
    @ResponseBody
    @RequestMapping(value = "/collection/isCollection", method = RequestMethod.POST)
    public String isCollection(String accessToken, Integer commodityId) {
        UserInfo user = userDao.getUserByToken(accessToken);
        Map<String,Object> map = new HashMap<>();
        String code = "2";
        String msg = "处理异常！";
        if (user != null) {
            if (collectionDao.isCollection(user.getUid(),commodityId)) {
                code = "1";
                msg = "该商品已收藏！";
                map.put("return","该商品已收藏！");
            } else {
                code = "1";
                msg = "该商品未收藏！";
                map.put("return","该商品未收藏！");
            }
        } else {
            msg = "未查询到用户信息！";
        }
        return resultMsg.mapToJson(code, msg, map);
    }
}
